package org.mobiletrain.www.secondproject2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
    private FragmentTransaction ft;

    private String cityName;
    private String cityCode;
    private String cityId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar);
        toolBar.setTitle("周边游");
        getSupportActionBar().hide();

        //得到从CheckPlaceActivity传过来的值
        //得到从CheckPlaceActivity传过来的值
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        cityCode = bundle.getString("cityCode");
        cityId = bundle.getString("cityId");
        cityName = bundle.getString("cityName");

        //Log.i(TAG, "===========+++++++" + cityCode);
       // Log.i(TAG,"===========+++++++"+cityName);


        TextView cName = (TextView) findViewById(R.id.tv3);
        cName.setText(cityName);

        ImageView placeListIcon = (ImageView) findViewById(R.id.place_list);
        placeListIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,CheckPlaceActivity.class);
                startActivity(i);

            }
        });


        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.shouye_group);
        RadioButton shouyeRadio = (RadioButton) radioGroup.getChildAt(0);
        shouyeRadio.setChecked(true);
        ft = getSupportFragmentManager().beginTransaction();

        ShouYeFragment shouYeFragment = new ShouYeFragment();
        //传值
        Bundle args = new Bundle();
        args.putString("cityCode",cityCode);
        args.putString("cityName",cityName);
        args.putString("cityId",cityId);
        shouYeFragment.setArguments(args);
        ft.add(R.id.fragment_container, shouYeFragment).commit();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                ft =getSupportFragmentManager().beginTransaction();
                Fragment f=null;
                switch (checkedId){
                    case R.id.shouye_radio:
                        f=new ShouYeFragment();
                        //传值
                        Bundle args = new Bundle();
                        args.putString("cityCode",cityCode);
                        args.putString("cityName",cityName);
                        args.putString("cityId",cityId);
                        f.setArguments(args);
                        break;
                    case R.id.nearby_radio:
                        f=new NearbyFragment();
                        Bundle argsNearby = new Bundle();
                        argsNearby.putString("cityCodeNearby",cityCode);
                        argsNearby.putString("cityNameNearby",cityName);
                        f.setArguments(argsNearby);

                        break;
                    case R.id.find_radio:
                        f=new FindFragment();
                        break;
                    case R.id.me_radio:
                        f=new MyFragment();
                        break;
                }
                ft.replace(R.id.fragment_container, f).commit();
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
